
//全局Express框架
const express = require("express");
const router = express.Router();
module.exports = router;

//加载配置文件
const config = require("../config/mysql");
const mysql = require("mysql2");
const conn = mysql.createConnection(config);

/**
 * @description 获取文章列表
 * 入参格式如下：
  {
    "page": {
      "size": 10,
      "num": 1
    },
    "params": {
      "typeId": 3,
      "typePid": 2
    }
  }
 */
router.all("/list", (req, res) => {
  if (req.method !== 'POST') {
    return res.json({ code: 403, message: "禁止访问" });
  }
  if (!req.body.page) {
    return res.json({ code: 403, message: "缺少分页入参" });
  }

  const orderBy = ' order by id desc'
  let whereStr = ' where 1=1';
  // 分类ID
  whereStr += ( req.body.params && req.body.params.typeId ) ? ` and type_id = ${req.body.params.typeId} ` : '';
  // 父分类ID
  whereStr += ( req.body.params && req.body.params.typePid ) ? ` and type_pid = ${req.body.params.typePid} ` : '';
  
  const pageSize = req.body.page.size || 10;  // 每页条数
  const pageNum = req.body.page.num || 1; // 页码
  const limitOffset = (pageNum - 1) * pageSize; // limit偏移量

  // 查询总条数SQL
  const countSqlStr = `select count(*) total from article`;
  conn.query(countSqlStr, (errCount, resultCount) => {
    if (errCount) {
      return res.json({ code: 404, message: "获取失败" });
    }
    const successResult = {
      code: 200,
      message: '',
      data: {
        page: {
          pageSize,
          pageNum,
          total: resultCount[0].total
        },
        lstItem: []
      }
    }
    // 查询列表SQL
    const sqlStr = `select id,title,type_pid,type_pname,type_id,type_name,description,create_time from article ${whereStr} ${orderBy} limit ${limitOffset},${pageSize}`;
    conn.query(sqlStr, (err, result) => {
      if (err) {
        return res.json({ code: 404, message: "获取失败" });
      }
      successResult.data.lstItem = result
      res.json(successResult);
    });
  });
});

/**
 * @description 获取文章详情
 * 入参格式如下：
  {
    "params": {
      "id": 3
    }
  }
 */
router.all("/item", (req, res) => {
  if (req.method !== 'POST') {
    return res.json({ code: 403, message: "禁止访问" });
  }
  if (!req.body.params || !req.body.params.id) {
    return res.json({ code: 403, message: "非法入参" });
  }

  let whereStr = ' where 1=1';
  // 文章ID
  whereStr += ` and id = ${req.body.params.id} `;

  // 查询列表SQL
  const sqlStr = `select link,id,title,type_pid,type_pname,type_id,type_name,description,content,create_time from article ${whereStr} limit 1`;
  conn.query(sqlStr, (err, result) => {
    if (err) {
      return res.json({ code: 404, message: "获取失败" });
    }
    
    const successResult = {
      code: 200,
      message: '',
      data: {
        item: result[0]
      }
    }
    res.json(successResult);
  });
});

/**
 * @description 根据上级栏目ID获取下级栏目
 * 入参格式如下：
  {
    "params": {
      "typePid": 1
    }
  }
 */
router.all("/sonType", (req, res) => {
  if (req.method !== 'POST') {
    return res.json({ code: 403, message: "禁止访问" });
  }
  if (!req.body.params || (typeof req.body.params.typePid === 'undefined') || (req.body.params.typePid*1 < 0)) {
    return res.json({ code: 403, message: "非法入参" });
  }

  const typePid = req.body.params.typePid*1;

  let sqlStr = '';
  let whereStr = ' where 1=1';
  if (typePid === 0) {
    // 顶级栏目
    // 查询列表SQL
    sqlStr += `select type_pname type_name,type_pid type_id,type_pname,type_pid from article ${whereStr} group by type_pname,type_pid`;
  } else {
    // 一级栏目ID
    whereStr += ` and type_pid = ${typePid} `;
    // 查询列表SQL
    sqlStr += `select type_name,type_id,type_pname,type_pid from article ${whereStr} group by type_name,type_id,type_pname,type_pid`;
  }
  conn.query(sqlStr, (err, result) => {
    if (err) {
      return res.json({ code: 404, message: "获取失败" });
    }
    
    const successResult = {
      code: 200,
      message: '',
      data: {
        lstItem: result
      }
    }
    res.json(successResult);
  });
});